Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

337
Vistas
select2 dropdownlist doesnt close after clicking the "add" button

I have added a "Add new person" button in a select2 dropdownlist

The select2 code looks like this:

  $('#subFundSelection').select2({
            width: '100%',
            placeholder: 'Select a sub-fund',
            ajax: {
                url: '/FundInfo/SearchLegalEntities',
                type: 'GET',
                dataType: 'json',
                delay: 250,
                allowClear: true,
                data: function (params) {
                    return {
                        searchTerm: params.term
                    };
                },
                processResults: function (data) {
                    return {
                        results: $.map(data.legalEntities, function (obj) {
                            return { id: obj.Id, text: obj.Text };
                        })
                    };
                },
            },
            minimumInputLength: 2
        }).on('select2:open', () => {
            $(".select2-results:not(:has(button))").append('<button id="newPersonButton" style="width: 100%" type="button" class="btn btn-primary" onClick="CreateLegalEntity()">+ Add a new person</button>')
        })

A button is well added to the dropdownlist (I cant add a picture sorry)

and when I click this "Add new person" button I open a modal.

function CreateLegalEntity() {
  
   $('#CreateLegalEntityModal').modal('show');
}

Everything work fine, instead of the fact that the dropdown list doesnt close after the click and it appears in the top of the modal, until I click somewhere else in the screen.

Sorry for not attaching screenshots but the website doesnt allow me.

I have tried:

$(this).blur() 
$(".select2").trigger('blur') 
$(".select2").trigger('change')
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

From the documentation of SELECT2 here Methods | Select2

The close method will cause the dropdown menu to close, hiding the selectable options:

$('#subFundSelection').select2('close');

Should do what your looking for

EDIT

You should be calling the .select2('close') method from the same identifier your are initially creating the element .select2({params});

eg: $('#div1').select2() so it should be $('#div1').select2('close')

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda